package com.CGFrame.struts.bean.project;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.CGFrame.struts.POJOs.Company;
import com.CGFrame.struts.POJOs.Employee;
import com.CGFrame.struts.POJOs.Project;
import com.CGFrame.struts.POJOs.Projectreguserrole;
import com.CGFrame.struts.POJOs.Reguser;
import com.CGFrame.struts.POJOs.Reguserrole;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

public class ProjectBean {
	private Project project;
	public ProjectBean(Project project)
	{
		this.project = project;
	}
	public ProjectBean(String projectId)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		this.project = (Project)pop.UniqueQuery("Project", "projectId,"+projectId);
	}
	
	public ProjectBean(){
		
	}
	public LinkedList getAllProjectId(){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		List list=pop.Query("Project",null, "projectId","asc");
		LinkedList<String[]> lnlist=new LinkedList<String[]>();
		for(Iterator iter=list.iterator();iter.hasNext();){
			Project pj=(Project)iter.next();
			String pro[]={String.valueOf(pj.getProjectId()),pj.getProjectName()};
			lnlist.add(pro);
		}
		return lnlist;
	}
	public Company getCompany()
	{
		return project.getCompany();
	}
	
	public Integer[] getReguserRoles()
	{
/*		LinkedHashSet lin_regUserRole = new LinkedHashSet();
		Iterator ite_proregrole = project.getProjectreguserroles().iterator();
		for(;ite_proregrole.hasNext();)
		{
			Projectreguserrole proRegRole = (Projectreguserrole)ite_proregrole.next();
			proRegRole.getReguserrole();
			lin_regUserRole.add(proRegRole.getReguserrole());
		}
		
		return lin_regUserRole;
*/	
		LinkedHashSet lin_regUserRole = new LinkedHashSet();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		List lis_ite_projectreguserrole = pop.Query("Projectreguserrole", "project.projectId,"+project.getProjectId(), "reguserrole.role.roleId", "ASC");
		int size = lis_ite_projectreguserrole.size();
		Integer arr_ProjectReguserRoles[] = new Integer [size] ;
		for (int i = 0 ; i<size;i++)
		{
			Projectreguserrole proreguserrole = (Projectreguserrole)lis_ite_projectreguserrole.get(i);
			arr_ProjectReguserRoles[i] = proreguserrole.getReguserrole().getRegUserRoleId();
		}
		return arr_ProjectReguserRoles;
	}
	public LinkedHashSet getRegusers()
	{
		LinkedHashSet lin_regUsers = new LinkedHashSet();

		Iterator ite_proregrole = project.getProjectreguserroles().iterator();
		for(;ite_proregrole.hasNext();)
		{
			Projectreguserrole proRegRole = (Projectreguserrole)ite_proregrole.next();
			Reguser reguser = proRegRole.getReguserrole().getReguser();
			if(!( lin_regUsers.contains(reguser) ) )
			{				
				reguser.setRegName(reguser.getRegName()+" ("+reguser.getEmployee().getEmpName()+")");
				lin_regUsers.add(reguser);
			}
		}
		return lin_regUsers;
	}
	public String getProjectId() {
		return this.project.getProjectId().toString();
	}
	public HashSet getEmployeeNameInProject(){
		HashSet<String> set=new HashSet<String>();
		Projectreguserrole pur;
		Reguserrole rr;
		Reguser user;
		Employee emp;
		String empName;
		Set set1=project.getProjectreguserroles();
		for(Iterator iter=set1.iterator();iter.hasNext();){
			pur=(Projectreguserrole)iter.next();
			rr=(Reguserrole)pur.getReguserrole();
			user=(Reguser)rr.getReguser();
			emp=user.getEmployee();
			empName=emp.getEmpName();
			set.add(empName);
		}
		return set;
	}
	
	public String getProjectName(){
		return project.getProjectName();
	}
	
	public Set getShotgroups(){
		return project.getShotgroups();
	}
	public Set getElementgroups(){
		return project.getElementgroups();
	}
	
	//判断项目名称是否可用,可用返回true,不可用返回false
	public boolean checkName(String name){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Project p=(Project)pop.UniqueQuery("Project", "projectName,"+name);
		if(p==null){
			return true;
		}else{
			return false;
		}
	}
	
	public String getProjectManager(){
		return project.getProjectManager();
	}
	public Project getProject()
	{
		return project;
	}
}
